package com.jk.service;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.jk.dao.OrderDao;
import com.jk.dao.ProductDao;
import com.jk.entity.*;
import com.jk.utils.PayUtils;
import com.jk.utils.PayUtils1;
import com.jk.utils.PayUtils2;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.*;
import java.util.concurrent.TimeUnit;

@Service
public class ProductService {
    @Autowired
    private ProductDao productDao;
     @Autowired
     private  RedisTemplate redisTemplate;

     @Autowired
     private OrderDao orderDao;

     @Autowired
     private MongoTemplate mongoTemplate;

    public List<Product> getListPro() {
            return productDao.getListPro();
        }


    public List<Product> getProOne(Integer id) {
        return productDao.getProOne(id);
    }

   public void gou(HttpServletResponse httpResponse,  HttpServletRequest request, String id,String cname, String cphone, String address,String couponId) throws IOException {
        Consignee  consignee= new Consignee();
        consignee.setConsignee_name(cname);
        consignee.setConsignee_phone(cphone);
        consignee.setConsignee_address(address);
        redisTemplate.opsForValue().set("consignne",consignee);
       Gson gson = new Gson();
       User user = (User) request.getSession().getAttribute("user");

        Product pro=productDao.getInfo(id);
       PayUtils1 payUtils1=new PayUtils1();

       String coupon= (String) redisTemplate.opsForHash().get("coupon",user.getUser_id());
       List<Coupon> list3=gson.fromJson(coupon,new TypeToken<List<Coupon>>(){}.getType());
       Double couponJ=0.0;
       int index =0;
       if(!couponId.equals("0")){
           for (int i = 0; i < list3.size(); i++) {
               if (couponId.equals(list3.get(i).getCoupon_id())){
                   couponJ=list3.get(i).getCoupon_discount();
                   index = i;
               }
           }
           list3.remove(index);
           redisTemplate.opsForHash().delete("coupon",user.getUser_id());
           Map<String, Object> map = new HashMap<>();
           map.put(user.getUser_id(),gson.toJson(list3));
           redisTemplate.opsForHash().putAll("coupon",map);

           if (pro.getProduct_price()<=couponJ){
               pro.setProduct_price(0.0);
           }else{
               pro.setProduct_price(pro.getProduct_price()-couponJ);
           }
       }
       payUtils1.alipay(httpResponse,pro.getProduct_price(),pro.getProduct_name(),pro.getProduct_description());
       redisTemplate.opsForValue().set("oid",id);


       SimpleDateFormat sim1=new SimpleDateFormat("yyyyMMddHHmmssSS");




       int floor =(int)Math.round(Math.random() * 899 + 100);

       String id1 = sim1.format(new Date())+""+floor+""+user.getUser_id()+""+id;

       Order order=new Order();
       order.setOrder_id(id1);


       order.setOrder_money(pro.getProduct_price());
       order.setOrder_receiverinfo(pro.getProduct_description());

       order.setUser_id(user.getUser_id());
       order.setUser_name(user.getUser_nickname());
       SimpleDateFormat sim=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
       order.setOrder_time(sim.format(new Date()));
       order.setOrder_paystate(1);
       order.setStateofpayment(1);
       order.setConsignee_address(address);
       order.setConsignee_name(cname);
       order.setConsignee_phone(cphone);

       OrderItem orderItem = new OrderItem();
       orderItem.setNum(1);
       orderItem.setProduct_price(pro.getProduct_price());
       orderItem.setOrder_id(id1);
       orderItem.setProduct_id(pro.getProduct_id());
       orderItem.setProduct_image(pro.getProduct_imgurl());
       orderItem.setProduct_name(pro.getProduct_name());


       OrderandItem orderandItem=new OrderandItem();
       orderandItem.setOrder_id(order.getOrder_id());
       orderandItem.setOrder_money(orderItem.getProduct_price());
       orderandItem.setOrder_receiverinfo(pro.getProduct_description());
       orderandItem.setUser_id(order.getUser_id());
       orderandItem.setOrder_time(order.getOrder_time());
       orderandItem.setOrder_paystate(order.getOrder_paystate());
       orderandItem.setStateofpayment(order.getStateofpayment());
       orderandItem.setConsignee_phone(order.getConsignee_phone());
       orderandItem.setConsignee_name(order.getConsignee_name());
       orderandItem.setConsignee_address(order.getConsignee_address());

       orderandItem.setNum(1);
       orderandItem.setProduct_price(orderItem.getProduct_price());
       orderandItem.setOrder_id(order.getOrder_id());
       orderandItem.setProduct_id(orderItem.getProduct_id());
       orderandItem.setProduct_image(orderItem.getProduct_image());
       orderandItem.setProduct_name(orderItem.getProduct_name());


       List  list = new ArrayList();
       list.add(orderandItem);
       Map<String, Object> map1 = new HashMap<>();
       map1.put(user.getUser_id(),gson.toJson(list));
       redisTemplate.opsForHash().putAll("order",map1);


       //redisTemplate.opsForHash().put("order",user.getUser_id(),order);
   }

    public List<Product> getListPros() {
        Gson gson = new Gson();
        //定义key
        String   cacheKey = "Product";

        Boolean hasKey = redisTemplate.hasKey(cacheKey);
        if(hasKey){
            String sir = (String) redisTemplate.opsForValue().get(cacheKey);
            List<Product> listPro = new ArrayList<>();
            listPro = gson.fromJson(sir, new TypeToken<List<Product>>() {
            }.getType());
            return listPro;
        }else {
            List<Product> listPro = productDao.getListPros();
                 redisTemplate.opsForValue().set(cacheKey,gson.toJson(listPro),'1', TimeUnit.SECONDS);
            return listPro;
             }
    }


    public List<ProductSeckill> seckill() {
        Query query=new Query();
        List<ProductSeckill> productSeckills = mongoTemplate.find(query, ProductSeckill.class);
        return  productSeckills;
    }

    public List<ProductSeckill> seckill22() {
        return productDao.seckill22();
    }

    public List<ProductSeckill> seckillOne(String id) {
        return productDao.seckillOne(id);
    }

    public void seckillProduct(HttpServletResponse httpResponse, Integer id, HttpServletRequest request, String cname, String address, String cphone) throws IOException {
        Consignee consignee = new Consignee();
        consignee.setConsignee_address(address);
        consignee.setConsignee_phone(cphone);
        consignee.setConsignee_name(cname);
        redisTemplate.opsForValue().set("consignne",consignee);
        ProductSeckill pro=productDao.seckill(id);
        PayUtils2 payUtils2=new PayUtils2();
        payUtils2.alipay(httpResponse,pro.getBuy_price(),pro.getName(),pro.getSell_point());
        redisTemplate.opsForValue().set("seckill",id);
    }

    public void seckillProductPay(HttpServletRequest request) {

        Integer id= (Integer) redisTemplate.opsForValue().get("seckill");
        ProductSeckill pro=productDao.seckill(id);
        productDao.seckillUpdate(id);
        User user = (User) request.getSession().getAttribute("user");
        Consignee consignne = (Consignee) redisTemplate.opsForValue().get("consignne");
        SimpleDateFormat sim1=new SimpleDateFormat("yyyyMMddHHmmssSS");
        int floor =(int)Math.round(Math.random() * 899 + 100);

        String id1 = sim1.format(new Date())+""+floor+""+user.getUser_id()+""+id;

        Order order=new Order();
        order.setOrder_id(id1);

        order.setOrder_money(pro.getBuy_price());
        order.setOrder_receiverinfo(pro.getSell_point());

        order.setUser_id(user.getUser_id());
        order.setUser_name(user.getUser_nickname());
        SimpleDateFormat sim=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        order.setOrder_time(sim.format(new Date()));
        order.setOrder_paystate(1);
        order.setStateofpayment(1);
        order.setConsignee_address(consignne.getConsignee_address());
        order.setConsignee_phone(consignne.getConsignee_phone());
        order.setConsignee_name(consignne.getConsignee_name());
        OrderItem orderItem = new OrderItem();
        orderItem.setNum(1);
        orderItem.setProduct_price(pro.getBuy_price());
        orderItem.setOrder_id(id1);
        orderItem.setProduct_id(pro.getItem_id());
        orderItem.setProduct_image(pro.getI_img());
        orderItem.setProduct_name(pro.getName());
        orderDao.addOrderItems(orderItem);
        orderDao.addorder(order);
        redisTemplate.delete(id);
    }

    public void Pay2(HttpServletRequest request) {

        Consignee consignne = (Consignee) redisTemplate.opsForValue().get("consignne");

        Gson gson=new Gson();
        String id= (String) redisTemplate.opsForValue().get("oid");

        User user = (User) request.getSession().getAttribute("user");
        Order order=new Order();
        OrderItem orderItem = new OrderItem();

        String order1 =(String) redisTemplate.opsForHash().get("order", user.getUser_id());
//        List<OrderandItem>  list=gson.fromJson(order1, new TypeToken<List<OrderandItem>>() {}.getType());
//        for (int i = 0; i < list.size(); i++) {
//
//            order.setOrder_id(list.get(i).getOrder_id());
//            order.setOrder_money(list.get(i).getProduct_price());
//            order.setOrder_receiverinfo(list.get(i).getOrder_receiverinfo());
//            order.setUser_id(list.get(i).getUser_id());
//            SimpleDateFormat sim=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
//            order.setOrder_time(sim.format(new Date()));
//            order.setOrder_paystate(1);
//            order.setStateofpayment(1);
//            order.setConsignee_phone(list.get(i).getConsignee_phone());
//            order.setConsignee_name(list.get(i).getConsignee_name());
//            order.setConsignee_address(list.get(i).getConsignee_address());
//
//            orderItem.setNum(1);
//            orderItem.setProduct_price(list.get(i).getProduct_price());
//            orderItem.setOrder_id(list.get(i).getOrder_id());
//            orderItem.setProduct_id(list.get(i).getProduct_id());
//            orderItem.setProduct_image(list.get(i).getProduct_image());
//            orderItem.setProduct_name(list.get(i).getProduct_name());
//
//        }
//        orderDao.addOrderItems(orderItem);
//        orderDao.addorder(order);
//        redisTemplate.delete(id);




 //        Gson gson=new Gson();
//        String id= (String) redisTemplate.opsForValue().get("oid");
//
//        User user = (User) request.getSession().getAttribute("user");
//
//
//        String order1 =(String) redisTemplate.opsForHash().get("order", user.getUser_id());
//        List<OrderandItem>  list=gson.fromJson(order1, new TypeToken<List<OrderandItem>>() {}.getType());
//
//
//
        Product pro=productDao.getInfo(id);

        if(id==pro.getProduct_id()){
            SimpleDateFormat sim1=new SimpleDateFormat("yyyyMMddHHmmssSS");
            int floor =(int)Math.round(Math.random() * 899 + 100);

            String id1 = sim1.format(new Date())+""+floor+""+user.getUser_id()+""+id;

            order.setOrder_id(id1);
            double v = pro.getProduct_price() * pro.getProduct_num();
            order.setOrder_money(v);
            order.setOrder_receiverinfo(pro.getProduct_description());

            order.setUser_id(user.getUser_id());
            order.setUser_name(user.getUser_nickname());
            SimpleDateFormat sim=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            order.setOrder_time(sim.format(new Date()));
            order.setOrder_paystate(1);
            order.setStateofpayment(1);
            order.setConsignee_name(consignne.getConsignee_name());
            order.setConsignee_phone(consignne.getConsignee_phone());
            order.setConsignee_address(consignne.getConsignee_address());

            orderItem.setNum(1);
            orderItem.setProduct_price(pro.getProduct_price());
            orderItem.setOrder_id(id1);
            orderItem.setProduct_id(pro.getProduct_id());
            orderItem.setProduct_image(pro.getProduct_imgurl());
            orderItem.setProduct_name(pro.getProduct_name());
            orderDao.addOrderItems(orderItem);
            orderDao.addorder(order);
            redisTemplate.delete(id);
        }

    }

    public List<Shoptype> getProOness(Integer id) {

        return productDao.getProOness(id);
    }

    public List<Shoptype> getTypeOne(Integer id) {
        return productDao.getTypeOne(id);
    }

    public List<Product> getPros1(Integer id) {

        return productDao.getPros1(id);
    }
}


